package net.fruit.mq.client;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.fastjson.JSON;

import lombok.extern.slf4j.Slf4j;
import net.fruit.mq.client.internal.Configure;

@Slf4j
public class MQConfig {
	private static String CFG = "mq.conf";
	
	private static Map<String, Config> _config = new ConcurrentHashMap<String, Config>();
	
	static {
	    Properties properties = Configure.loadConfig(CFG);
	    Set<String> keys = properties.stringPropertyNames();
	    for (String key : keys) {
	        String[] arr = key.split("[.]");
	        if (arr.length < 3) {
	            continue;
	        }
	        String channel = arr[1];
            Config config = _config.get(channel);
            if (config == null) {
                config = new Config(channel);
                _config.put(channel, config);
            }
            if (arr.length == 3) {
                config.put(arr[2], properties.getProperty(key), 0);
            } else if (arr.length == 4) {
                if ("producer".equals(arr[2])) {
                    config.put(arr[3], properties.getProperty(key), 1);
                } else if ("consumer".equals(arr[2])) {
                    config.put(arr[3], properties.getProperty(key), 2);
                }
            } else if (arr.length == 5) {
                if ("consumer".equals(arr[2]) && "class".equals(arr[4])) {
                    config.putConsumerClass(arr[3], properties.getProperty(key));
                }
            }
	    }
	    
	    for (Map.Entry<String, Config> entry : _config.entrySet()) {
	        log.info("load message queue configure({}): {}", entry.getKey(), JSON.toJSONString(entry.getValue()));
	    }
	}
	
	public static Set<String> stringChannelNames() {
	    return _config.keySet();
	}
	
	public static Properties getProducerProperties(String channel) {
	    Config cfg = _config.get(channel);
	    return cfg == null ? new Properties() : cfg.getProducerProperties();
	}
	
	public static Properties getConsumerProperties(String channel) {
        Config cfg = _config.get(channel);
        return cfg == null ? new Properties() : cfg.getConsumerProperties();
    }

    public static Map<String, String> getConsumerClass(String channel) {
        Config cfg = _config.get(channel);
        return cfg == null ? new HashMap<String, String>(0) : cfg.getConsumerClass();
    }
    
	static class Config {
        String channel;
        Properties producerCfg = new Properties();
        Properties consumerCfg = new Properties();
        Map<String, String> consumerClass = new ConcurrentHashMap<String, String>();
        
        public Config(String channel) {
            this.channel = channel;
        }
        
        public void put(String key, Object value, int n) {
            if (value == null || value.toString().length() == 0) {
                return;
            }
            if (n == 0 || n == 1) {
                producerCfg.put(key, value);
            }
            if (n == 0 || n == 2) {
                consumerCfg.put(key, value);
            }
        }
        
        public void putConsumerClass(String topic, String className) {
            consumerClass.put(topic, className);
        }
        
        public Properties getProducerProperties() {
            return this.producerCfg;
        }
        
        public Properties getConsumerProperties() {
            return this.consumerCfg;
        }
        
        public Map<String, String> getConsumerClass() {
            return this.consumerClass;
        }
    }
	
	public static void main(String[] args) {
	}
}
